/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.bo;

import com.comut.crud.pojo.user.User;
import com.ecwid.mailchimp.MailChimpObject;
import com.ecwid.mailchimp.method.list.ListSubscribeMethod;
import com.ecwid.mailchimp.method.list.ListUnsubscribeMethod;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
public class NotificationHandler {

    private static Logger _logger = Logger.getLogger(NotificationHandler.class.getName());
    private static final ResourceBundle rscBundle = ResourceBundle.getBundle("application");
    private static final String _fullName = rscBundle.getString("fullName");
    private static final String _email = rscBundle.getString("email");
    private static final String _tags = rscBundle.getString("tags");
    private static final String _cepUrl = rscBundle.getString("cepUrl");
    private static final String _cepUpdateDrivers = rscBundle.getString("cepUpdateDrivers");
    private static final String _cepUpdateGroups = rscBundle.getString("cepUpdateGroups");
    private static final String _cepUpdatePois = rscBundle.getString("cepUpdatePois");
    private static final String _cepUpdateTrailers = rscBundle.getString("cepUpdateTrailers");
    private static final String _cepUpdateVehicles = rscBundle.getString("cepUpdateVehicles");
    private static final String _tomUrl = rscBundle.getString("tomUrl");
    private static final String _tomGroupHandler = rscBundle.getString("tomGroupHandler");
    public static final int TomCRUDCreate = Integer.parseInt(rscBundle.getString("tomCRUDCreate"));
    public static final int TomCRUDUpdate = Integer.parseInt(rscBundle.getString("tomCRUDUpdate"));
    public static final int TomCRUDDelete = Integer.parseInt(rscBundle.getString("tomCRUDDelete"));

    public static void notifyAboutPois() {
        notifyCep(_cepUpdatePois);
    }

    public static void notifyAboutDrivers() {
        notifyCep(_cepUpdateDrivers);
    }

    public static void notifyAboutGroups() {
        notifyCep(_cepUpdateGroups);
    }

    public static void notifyAboutTrailers() {
        notifyCep(_cepUpdateTrailers);
    }

    public static void notifyAboutVehicles() {
        notifyCep(_cepUpdateVehicles);
    }

    private static void notifyCep(String entity) {
        String urlString = null;
        try {
            //get the url
            urlString = _cepUrl + entity;

            notifyUrl(urlString);

        } catch (Exception ex) {
            _logger.error("error invoking CEP: " + urlString, ex);
        }
    }

    public static void notifyAboutUsersNew(User user) {
        ListSubscribeMethod listSubscribeMethod = MailChimpClientCRUD.getInstance().getListSubscribeMethod();
        listSubscribeMethod.email_address = user.getEmail();
        MailChimpObject mco = new MailChimpObject();
        mco.put(_email, user.getEmail());
        mco.put(_fullName, user.getName());
        mco.put(_tags, user.getTags());
        listSubscribeMethod.merge_vars = mco;
        MailChimpClientCRUD.getInstance().execute(listSubscribeMethod);
    }

    public static void notifyAboutUsersUpdate(String cuurentEmail, User user) {
        ListSubscribeMethod listSubscribeMethod = MailChimpClientCRUD.getInstance().getListSubscribeMethod();
        listSubscribeMethod.email_address = cuurentEmail;
        MailChimpObject mco = new MailChimpObject();
        if (user.getEmail() != null) {
            mco.put(_email, user.getEmail());
        }
        if (user.getName() != null) {
            mco.put(_fullName, user.getName());
        }
        if (user.getTags() != null) {
            mco.put(_tags, user.getTags());
        }
        listSubscribeMethod.merge_vars = mco;
        MailChimpClientCRUD.getInstance().execute(listSubscribeMethod);
    }

    public static void notifyAboutUsersDelete(String email) {
        ListUnsubscribeMethod listUnsubscribeMethod = MailChimpClientCRUD.getInstance().getListUnsubscribeMethod();
        listUnsubscribeMethod.email_address = email;
        MailChimpClientCRUD.getInstance().execute(listUnsubscribeMethod);

    }

    /**
     *
     * @param optId the operation id (create=0; update=1; delete=3)
     * @param groupId the group id
     */
    public static void notifyTOMAboutGroups(int optId, int groupId) {
        final String urlString;
        try {

            Object[] params = new Object[]{optId, groupId};
            // get the url
            urlString = _tomUrl + MessageFormat.format(_tomGroupHandler, params);

            notifyUrl(urlString);
            _logger.info("urlString: " + urlString);

        } catch (Exception ex) {
           _logger.error("error invoking TOM",ex);
        }
    }

    private static void notifyUrl(final String urlString) {
        Runnable runnable = new Runnable() {

            public void run() {
                URL url;
                try {
                    url = new URL(urlString);
                    url.openStream();
                } catch (Exception e) {
                    _logger.error("urlString: "+urlString,e);
                }
            }
        };

        (new Thread(runnable)).start();
    }
}
